<?php
/**
 * Created by jinqiang on 2024/7/26 9:39.
 * Copyright (c) 2024, Your Company. All rights reserved.
 */


namespace App\Http\Controllers\Fd;


use App\Http\Controllers\Controller;
use App\Http\Services\Fd\InvoiceService;
use Illuminate\Http\Request;

class InvoiceController extends Controller
{
    public function list(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required',
            'type' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->search($param);
        return result($res);
    }

    //待开票列表
    public function kpList(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required',
            'type' => 'required',
            'groups' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->searchKp($param);
        return result($res);
    }

    //获取多合一列表
    public function moreList(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'page' => 'required',
            'limit' => 'required',
            'type' => 'required',
        ]);
        $param = $request->all();
        $res = $invoiceService->searchMore($param);
        return result($res);
    }

    public function shops(Request $request, InvoiceService $invoiceService)
    {
        $res = $invoiceService->getShop();
        return result($res);
    }

    //发票详情
    public function detail(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'sourceId' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->detail($param['sourceId']);
        return result($res);
    }

    //冲红详情--单开、多开
    public function chDetail(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'sourceId' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->chDetail($param['sourceId']);
        return result($res);
    }

    //新开票详情
    public function newDetail(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'sourceId' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->newDetail($param['sourceId']);
        return result($res);
    }

    //发起冲红申请
    public function applyCH(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'ch_status' => 'required|in:1,2',
            'sourceIds' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->applyCH($param);
        return $res['code'] === 0 ? success("申请成功") : error($res['msg'], $res);
    }

    //开票、冲红
    public function setKpch(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'groups' => 'required|in:1,2',//1 开票 2 冲红
            'sourceIds' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->setKpch($param['groups'], $param['sourceIds']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //开票、冲红(多开)
    public function setKpchMore(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'groups' => 'required|in:1,2',//1 开票 2 冲红
            'merge_tags' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->setKpchMore($param['groups'], $param['merge_tags']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //设置开票信息、再次申请开票
    public function setData(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'type' => 'required|in:0,1',//1 开票 2 无需开票
            'sourceIds' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->setData($param);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //申请多单合一开票、修改多单合一开票信息
    public function setMoreData(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'type' => 'required|in:1,2',//1 单张 2 多合一
            'sourceIds' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->setMoreData($param);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }

    //取消多合一开票
    public function cancelMore(Request $request, InvoiceService $invoiceService)
    {
        $this->validate($request, [
            'merge_tags' => 'required'
        ]);
        $param = $request->all();
        $res = $invoiceService->cancelMore($param['merge_tags']);
        return $res['code'] === 0 ? success("操作成功") : error($res['msg'], $res);
    }
}
